package com.css.gitstats.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.models.CommitAction;
import org.gitlab4j.api.models.CommitAction.Action;
import org.gitlab4j.api.models.CommitPayload;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.RepositoryArchiveParams;

import com.google.common.collect.Lists;

import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;

public class GitLabImportExample {
	
    public static void main(String[] args) {
        try (// 创建一个 GitLabApi 实例
		GitLabApi gitLabApi = new GitLabApi("http://192.168.94.1:3000", "glpat-64puUdmdZwwvqxQuncLo")) {
            try {  
                // 创建新项目的参数  
                Project project = gitLabApi.getProjectApi().getProject("sheezer2/towel");
                
                CommitPayload payload = new CommitPayload();
                payload.setAuthorEmail("liuwenlong@126.com");
                payload.setBranch("main");
                payload.setCommitMessage("xxx");
                List<CommitAction> actions = Lists.newArrayList();
                CommitAction ca = new CommitAction();
                ca.setAction(Action.CREATE);
                ca.setContent(IoUtil.read(new FileInputStream(new File("C:\\Users\\liuwe\\AppData\\Local\\Temp\\project-17760837265227465970\\src\\main\\java\\com\\css\\towel\\TowelApplication.java")), StandardCharsets.UTF_8));
                ca.setFilePath("ccc/main/java/com/css/towel/TowelApplication.java");
                ca.setPreviousPath("ccc/main/java/com/css/towel");
                actions.add(ca);
                payload.setActions(actions);
                 gitLabApi.getCommitsApi().createCommit(project.getId(), payload);
            } catch (GitLabApiException e) {  
                e.printStackTrace();  
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void exportProject(GitLabApi gitLabApi,Project project) throws GitLabApiException, IORuntimeException, FileNotFoundException {
    	RepositoryArchiveParams params = new RepositoryArchiveParams();
        InputStream is = gitLabApi.getRepositoryApi().getRepositoryArchive(project.getId(), params, "tar.gz");
        IoUtil.copy(is, new FileOutputStream(new File("D:\\sourcecode\\soft-factory\\admin\\fresco-plus\\sql\\test.tar.gz")));
    }
}
